<template>
	<view>
		<u-loading-page :loading="isloading"></u-loading-page>
		<view v-if="!isloading">
			<view class="content" v-if="!hasSubmit">
				<view class="business">
					<text class="text">1.请上传正面</text>
					<!-- <image v-if="ImgUrl" class="business_license" :src="chooseImg1?chooseImg1:ImgUrl+'positive.jpg'"
						mode="aspectFit"></image> -->
						<view class="business_license">
							
						</view>
					<u-icon name="plus" color="#2979ff" size="28" bold top="-100" @click="handleChooseImg(1)"></u-icon>
				</view>
				<view class="business">
					<text class="text">2.请上传反面</text>
					<!-- <image v-if="ImgUrl" class="business_license" :src="chooseImg2?chooseImg2:ImgUrl+'narrow.jpg'"
						mode="aspectFit"></image> -->
						<view class="business_license">
							
						</view>
					<u-icon name="plus" color="#2979ff" size="28" bold top="-100" @click="handleChooseImg(2)"></u-icon>
				</view>
				<view class="business">
					<text class="text">3.请上传执照</text>
					<!-- <image v-if="ImgUrl" class="business_license"
						:src="chooseImg3?chooseImg3:ImgUrl+'business-license.jpg'" mode="aspectFit"></image> -->
						<view class="business_license">
							
						</view>
					<u-icon name="plus" color="#2979ff" size="28" bold top="-100" @click="handleChooseImg(3)"></u-icon>
				</view>
				<u-alert :title="title" type="warning" description="收集的信息仅用作商户认证" center></u-alert>
				<u-button type="primary" text="提交" :loading="isloading" loadingText="加载中" @click="submitt"></u-button>
			</view>
			<view v-else>
				<u-empty v-if="status==0" mode="data" icon="http://cdn.uviewui.com/uview/empty/data.png" text="审核中!"
					textSize="28">
				</u-empty>
				<u-empty v-if="status==1" mode="data" icon="http://cdn.uviewui.com/uview/empty/data.png" text="审核通过!"
					textSize="28">
				</u-empty>
				<u-empty v-if="status==-1" mode="data" icon="http://cdn.uviewui.com/uview/empty/data.png" text="审核未通过!"
					textSize="28">
				</u-empty>
				<view class="steps">
					<u-steps :current="current" dot>
						<u-steps-item title="已提交"></u-steps-item>
						<u-steps-item title="审核中"></u-steps-item>
						<u-steps-item error title="审核未通过" v-if="!isPass"></u-steps-item>
						<u-steps-item title="已完成" v-else></u-steps-item>
					</u-steps>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				isloading: false,
				hasSubmit: false,
				status: '',
				current: 1,
				chooseImg1: '',
				chooseImg2: '',
				chooseImg3: '',
				isPass: true,
				ImgUrl: '',
				BaseUrl: '',
				token: '',
				isloading: true
			}
		},
		onLoad() {
			this.ImgUrl = getApp().globalData.ImgUrl
			this.BaseUrl = getApp().globalData.BaseUrl
			this.token = uni.getStorageSync('token')
			this.init()
		},
		methods: {
			async init() {
				let id = uni.getStorageSync("userinfo").id
				await uni.request({
					url: this.BaseUrl + '/identity/result',
					header: {
						'Content-Type': 'application/json',
						'token': this.token
					},
					data: {
						userId: id
					},
					success: (res) => {
						console.log("result", res)
						if (res.data.data) {
							this.status = res.data.data.status
							if (this.status == 0) {
								this.current = 1;
							} else if (this.status == -1) {
								this.current = 2;
								this.isPass = false
							} else if (this.status == 1) {
								this.current = 2;
								this.isPass = true
							}
							this.hasSubmit = true
						}
						this.isloading = false
						if (this.status == -1) {
							setTimeout(() => {
								uni.showToast({
									title: "2秒后重新上传",
									icon: "none"
								})
							}, 1000)
							setTimeout(() => {
								uni.showToast({
									title: "1秒后重新上传",
									icon: "none"
								})
							}, 2000)
							setTimeout(() => {
								this.hasSubmit = false
								this.current=1
								this.isPass=true
							}, 3000)
						}
					}
				})
			},
			handleChooseImg(number) {
				// 2 调用小程序内置的选择图片api
				uni.chooseImage({
					// 同时选中的图片的数量
					count: 1,
					// 图片的格式  原图  压缩
					sizeType: ['original', 'compressed'],
					// 图片的来源  相册  照相机
					sourceType: ['album', 'camera'],
					success: (result) => {
						console.log(result)
						wx.compressImage({
							src: result.tempFilePaths[0],
							quality: 30,
							success: (ress) => {
								if (number == 1) {
									this.chooseImg1 = ress.tempFilePath
								} else if (number == 2) {
									this.chooseImg2 = ress.tempFilePath
								} else {
									this.chooseImg3 = ress.tempFilePath
								}
							}
						})
					}
				});
			},
			submitt() {
				if (this.chooseImg1 && this.chooseImg2 && this.chooseImg3) {
					let imgLists = []
					imgLists.push(this.chooseImg1)
					imgLists.push(this.chooseImg2)
					imgLists.push(this.chooseImg3)
					let flag = true
					for (let index = 1; index <= 3; index++) {
						setTimeout(()=>{
							uni.uploadFile({
								url: this.BaseUrl + '/identity/upload',
								filePath: imgLists[index - 1],
								name: "file",
								header: {
									"Content-Type": "multipart/form-data",
									'token': this.token
								},
								formData: {
									code: index
								},
								success: (res) => {
									console.log(res)
									// if (JSON.parse(res.data).code != 200)
									// 	flag = false
									if (index == 3) {
										this.hasSubmit = !this.hasSubmit
									}
								}
							})
						},(index-1)*500)
						
					}
				}
			}
		}
	}
</script>

<style scoped lang="scss">
	.content {
		height: 100%;

		.business {
			position: relative;
			left: 50%;
			transform: translate(-50%);
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			width: 80%;
			height: 475upx;

			.text {
				position: relative;
				top: 10upx;
				left: 10upx;
				font-weight: bold;
			}

			.business_license {
				width: 600upx;
				height: 400upx;
				position: relative;
				top: 20upx;
				box-shadow: 0 0 10upx #888;
			}
		}
	}

	.steps {
		margin-top: 50upx;
	}
</style>
